﻿/* App.js -------------------------------------------------------------------
 *
 * Informatix Football Apps
 * QB Passing Efficiency Calculator (SharePoint App)
 *
 * © 2013 Jason Barkes - http://jbarkes.blogspot.com
 ----------------------------------------------------------------------------*/

/* SharePoint */
var context;
var web;

/* This function executes after the DOM is ready and SP scripts are loaded.
-----------------------------------------------------------------------------*/
function sharePointReady() {

    /* Calculation results panel */
    $(".qbr-calcs-panel").hide();
    $(".show_calcs").show();

    /* Calc panel event handlers */
    $('.show_calcs').click(function () {
        $(".qbr-calcs-panel").slideToggle();
    });

    /* Input control text selection - same as tab behavior */
    $("input[type=text], textarea").focus(function () {
        this.select();
    });
}

/* This function prepares, loads and executes a SharePoint query to get the
   current user's information.
-----------------------------------------------------------------------------*/
function getUserName() {
    user = web.get_currentUser();
    context.load(user);
    context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

/* This function is executed if the getUserName CSOM call is successful.
-----------------------------------------------------------------------------*/
function onGetUserNameSuccess() {
    $('#message').text('Welcome ' + user.get_title());
}

/* This function is executed if the getUserName SharePoint CSOM call fails.
-----------------------------------------------------------------------------*/
function onGetUserNameFail(sender, args) {
    alert('Failed to get user name. Error:' + args.get_message());
}

/* This function verifies the passing stats entered by the user.
-----------------------------------------------------------------------------*/
function verify_stats(comps, atts, yards, tds, ints) {
    if (!(comps) || (comps == "") || isNaN(comps)) {
        alert('Please specify the number of passing completions.');
        $('#ctl_completions').focus();
        return false;
    }
    if (!(atts) || (atts == "") || isNaN(atts) || atts <= 0) {
        alert('Please specify the number of passing attempts.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (!(yards) || (yards == "") || isNaN(yards)) {
        alert('Please specify the number of passing yards.');
        $('#ctl_yards').focus();
        return false;
    }
    if (!(tds) || (tds == "") || isNaN(tds)) {
        alert('Please specify the number of passing TDs.');
        $('#ctl_tds').focus();
        return false;
    }
    if (!(ints) || (ints == "") || isNaN(ints)) {
        alert('Please specify the number of interceptions.');
        $('#ctl_ints').focus();
        return false;
    }
    if (parseInt(atts) < parseInt(comps)) {
        alert('Attempts must be greater than or equal to the number of completions.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (parseInt(atts) < (parseInt(comps) + parseInt(ints))) {
        alert('Attempts must be greater than or equal to the total number of completions and interceptions.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (parseInt(comps) < parseInt(tds)) {
        alert('Completions must be greater than or equal to the number of passing TDs.');
        $('#ctl_completions').focus();
        return false;
    }

    return true;
}

/* This function calculates the NFL and NCAA QB passer efficiency ratings
   and displays the results.
-----------------------------------------------------------------------------*/
function calc_ratings() {
    var comps = $('#ctl_completions').val(),
        atts = $('#ctl_attempts').val(),
        yards = $('#ctl_yards').val(),
        tds = $('#ctl_tds').val(),
        ints = $('#ctl_ints').val();
    var cp, ya, tp, ip;
    var nfl_cpc, nfl_yac, nfl_tpc, nfl_ipc, nfl_cpa, nfl_yaa, nfl_tpa, nfl_ipa;
    var ncaa_cpc, ncaa_yac, ncaa_tpc, ncaa_ipc;
    var adj_rating, nfl_rating, ncaa_rating;

    /* Verify passing stats */
    if (verify_stats(comps, atts, yards, tds, ints) == false) {
        return false;
    }

    /* Completion Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    cp = (comps / atts) * 100;
    nfl_cpa = nfl_cpc = (cp - 30) * 0.05;
    if (nfl_cpc < 0) {
        nfl_cpa = 0
    }
    else if (nfl_cpc > 2.375) {
        nfl_cpa = 2.375;
    }
    ncaa_cpc = cp;

    $('#ctl_cp').val(cp.toFixed(2));
    $('#ctl_cpComp').val(nfl_cpc.toFixed(2));
    $('#ctl_cpFormula').val(nfl_cpa.toFixed(3));
    $('#ctl_cpNcaaComp').val(ncaa_cpc.toFixed(2));

    /* Yards per Attempt (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    ya = yards / atts;
    nfl_yaa = nfl_yac = (ya - 3) * 0.25;
    if (nfl_yac < 0) {
        nfl_yaa = 0
    }
    else if (nfl_yac > 2.375) {
        nfl_yaa = 2.375;
    }

    ncaa_yac = ya * 8.4;

    $('#ctl_ya').val(ya.toFixed(2));    
    $('#ctl_yaComp').val(nfl_yac.toFixed(3));
    $('#ctl_yaFormula').val(nfl_yaa.toFixed(3));
    $('#ctl_yaNcaa').val(ya.toFixed(2));
    $('#ctl_yaNcaaComp').val(ncaa_yac.toFixed(2));

    /* TD Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    tp = (tds / atts) * 100;
    nfl_tpa = nfl_tpc = (tp * 0.2);
    if (nfl_tpc > 2.375) {
        nfl_tpa = 2.375;
    }
    ncaa_tpc = tp * 3.3;

    $('#ctl_tp').val(tp.toFixed(2));
    $('#ctl_tpComp').val(nfl_tpc.toFixed(3));
    $('#ctl_tpFormula').val(nfl_tpa.toFixed(3));
    $('#ctl_tpNcaa').val(tp.toFixed(2));
    $('#ctl_tpNcaaComp').val(ncaa_tpc.toFixed(2));

    /* Interception Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    ip = (ints / atts) * 100;
    nfl_ipa = nfl_ipc = 2.375 - (ip * 0.25);
    if (nfl_ipc < 0) {
        nfl_ipa = 0;
    }
    ncaa_ipc = ip * 2;

    $('#ctl_ip').val(ip.toFixed(2));
    $('#ctl_ipComp').val(nfl_ipc.toFixed(3));
    $('#ctl_ipFormula').val(nfl_ipa.toFixed(3));
    $('#ctl_ipNcaa').val(ip.toFixed(2));
    $('#ctl_ipNcaaComp').val(ncaa_ipc.toFixed(2));

    /* Final calculations
    -------------------------------------------------------------------------*/

    /* NFL calculation values */
    $('#ctl_cpFinal').val(nfl_cpa.toFixed(3));
    $('#ctl_yaFinal').val(nfl_yaa.toFixed(3));
    $('#ctl_tpFinal').val(nfl_tpa.toFixed(3));
    $('#ctl_ipFinal').val(nfl_ipa.toFixed(3));

    /* NCAA calculation values */
    $('#ctl_cpNcaaFinal').val(ncaa_cpc.toFixed(2));
    $('#ctl_yaNcaaFinal').val(ncaa_yac.toFixed(2));
    $('#ctl_tpNcaaFinal').val(ncaa_tpc.toFixed(2));
    $('#ctl_ipNcaaFinal').val(ncaa_ipc.toFixed(2));

    /* Final NFL rating */
    adj_rating = (nfl_cpa + nfl_yaa + nfl_tpa + nfl_ipa) / 6;
    nfl_rating = adj_rating * 100;
    $('#ctl_nflRating').val(nfl_rating.toFixed(1));
    $('#ctl_nflRatingAdj').val(adj_rating.toFixed(3));
    $('#ctl_nflRating_lt').val(nfl_rating.toFixed(1));

    /* Final NCAA rating */
    ncaa_rating = ncaa_cpc + ncaa_yac + ncaa_tpc - ncaa_ipc;
    $('#ctl_ncaaRating').val(ncaa_rating.toFixed(1));
    $('#ctl_ncaaRating_lt').val(ncaa_rating.toFixed(1));
}
